package ua.org.enginer.controls {
	
	import flash.events.Event;
	
	import mx.controls.TextInput;
	
	[Event(name="allowNegativeChange")]
	[Event(name="allowDoubleChange")]
	
	public class NumberInput extends TextInput {
		
		
		private var _allowNegative:Boolean = true;
		
		public function set allowNegative(value:Boolean):void {
			if (value != _allowNegative) {
				_allowNegative = value;
				invalidateProperties()
				dispatchEvent (new Event("allowNegativeChange"));
			}
		}

		[Bindable(event="allowNegativeChange")]
		[Inspectable(category="General", enumeration="true,false", defaultValue="true")]
		public function get allowNegative():Boolean {
			return _allowNegative;
		}
		
		private var _allowDouble:Boolean = true;
		
		public function set allowDouble(value:Boolean):void {
			if (value != _allowDouble) {
				_allowDouble = value;
				invalidateProperties()
				dispatchEvent (new Event("allowDoubleChange"));
			}
		}

		[Bindable(event="allowDoubleChange")]
		[Inspectable(category="General", enumeration="true,false", defaultValue="true")]
		public function get allowDouble():Boolean {
			return _allowDouble;
		}


		
		override public function set text(value:String):void {
			super.text = Number(value) ? value : ""
		}
		
		override public function get text():String {
			//if (super.text == "--") super.text = "-"
			return super.text == "-" ?  "" : super.text.replace(",",".")
		}
		
		override public function set restrict(value:String):void {}
		
		override protected function commitProperties():void {
			var restrict:String = "0-9"
			restrict = allowNegative ? "-"+restrict : restrict
			restrict += allowDouble ? ".," : ""
			
			super.restrict = restrict
			super.commitProperties()
		}
		
		
		public function set value(value:Number):void {
			text = value.toFixed(2)
		}

		[Bindable("textChanged")]
	    [CollapseWhiteSpace]
	    [Inspectable(category="General", defaultValue="")]
	    [NonCommittingChangeEvent("change")]
		public function get value():Number {
			return Number(text);
		}
	}
}

